package com.lexue.articlesync.controller;

import com.lexue.articlesync.adapter.impl.ToutiaoAdapter;
import com.lexue.articlesync.entity.PlatformAuth;
import com.lexue.articlesync.service.ImageHostingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 图片上传测试控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/test")
@RequiredArgsConstructor
public class ImageUploadTestController {

    private final ImageHostingService imageHostingService;
    private final ToutiaoAdapter toutiaoAdapter;

    /**
     * 测试本地图片托管
     */
    @PostMapping("/host-image")
    public ResponseEntity<?> testHostImage(@RequestParam String imagePath) {
        try {
            String hostedUrl = imageHostingService.hostLocalImage(imagePath);
            if (hostedUrl != null) {
                return ResponseEntity.ok()
                        .body("图片托管成功: " + hostedUrl);
            } else {
                return ResponseEntity.badRequest()
                        .body("图片托管失败: " + imagePath);
            }
        } catch (Exception e) {
            log.error("测试图片托管失败", e);
            return ResponseEntity.internalServerError()
                    .body("测试失败: " + e.getMessage());
        }
    }

    /**
     * 测试头条号图片上传API
     */
    @PostMapping("/toutiao-image-upload")
    public ResponseEntity<?> testToutiaoImageUpload(
            @RequestParam String imageUrl,
            @RequestParam String cookies) {
        try {
            // 创建测试用的认证信息
            PlatformAuth auth = new PlatformAuth();
            auth.setCookies(cookies);
            
            // 调用私有方法进行测试需要使用反射，这里简化为返回指导信息
            String testUrl = "https://mp.toutiao.com/spice/image?upload_source=20020003&aid=1231&device_platform=web&need_cover_url=1";
            
            return ResponseEntity.ok()
                    .body("头条号图片上传API测试准备就绪\n" +
                          "测试URL: " + testUrl + "\n" +
                          "图片URL: " + imageUrl + "\n" +
                          "请使用浏览器开发者工具或Postman进行测试\n" +
                          "请求格式: POST " + testUrl + "\n" +
                          "表单数据: imageUrl=" + imageUrl + "\n" +
                          "请求头: Cookie=" + cookies.substring(0, Math.min(50, cookies.length())) + "...");
                          
        } catch (Exception e) {
            log.error("准备头条号图片上传测试失败", e);
            return ResponseEntity.internalServerError()
                    .body("测试准备失败: " + e.getMessage());
        }
    }

    /**
     * 获取测试用图片URL
     */
    @GetMapping("/sample-image")
    public ResponseEntity<?> getSampleImageUrl() {
        // 创建一个简单的测试图片URL
        String sampleUrl = "https://picsum.photos/800/600.jpg";
        return ResponseEntity.ok()
                .body("测试图片URL: " + sampleUrl + "\n" +
                      "这是一个来自Lorem Picsum的随机图片，可用于测试头条号图片上传功能");
    }

    /**
     * 清理托管的图片
     */
    @PostMapping("/cleanup-images")
    public ResponseEntity<?> cleanupImages() {
        try {
            imageHostingService.cleanupExpiredImages();
            return ResponseEntity.ok("图片清理完成");
        } catch (Exception e) {
            log.error("清理图片失败", e);
            return ResponseEntity.internalServerError()
                    .body("清理失败: " + e.getMessage());
        }
    }
}